//
//  OrderCell.m
//  MyNewProject
//
//  Created by 孙 on 2019/2/7.
//  Copyright © 2019 sun. All rights reserved.
//

#import "OrderCell.h"
@interface OrderCell()
@property(nonatomic, strong) UIView * bgView;
@property(nonatomic, strong) UIImageView * picImageView;
@property(nonatomic, strong) UILabel * titleLab;
@property(nonatomic, strong) UILabel * amountLab;


@property(nonatomic, strong) UIButton *complexBtn;
@property(nonatomic, strong) UIButton *repayBtn; // 退款按钮
@property(nonatomic, strong) UILabel *stateLab;

@end
@implementation OrderCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}
-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.contentView.backgroundColor = SKRGBColor(237, 237, 237);
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self initView];
    }
    return self;


}
-(void)initView
{
    [self.contentView addSubview:self.bgView];
}
-(UIView*)bgView
{
    if (!_bgView) {
        _bgView = [[UIView alloc] initWithFrame:CGRectMake(15, 10, SCREEN_WIDTH-30, 110)];
        _bgView.backgroundColor =[UIColor whiteColor];
    }
    return _bgView;
}
-(UIImageView*)picImageView
{
    if (!_picImageView) {
        _picImageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 15, 80, 80)];
        [self.bgView addSubview:_picImageView];
    }
    return _picImageView;
}
-(UILabel*)titleLab
{
    if (!_titleLab) {
        _titleLab = [[UILabel alloc] initWithFrame:CGRectMake(self.picImageView.right +11, self.picImageView.top, self.bgView.width - self.picImageView.right - 11 -20, 40)];
        _titleLab.font = SKFONT_Semibold(14);
        _titleLab.numberOfLines = 2;
        _titleLab.textAlignment = NSTextAlignmentLeft;
        _titleLab.lineBreakMode = NSLineBreakByTruncatingTail;
        _titleLab.textColor = SKColorWithHex(0x333333);
        [self.bgView addSubview:_titleLab];
    }
    return _titleLab;
}
-(UILabel*)timeLab
{
    if (!_timeLab) {
        _timeLab = [[UILabel alloc] initWithFrame:CGRectMake(self.picImageView.right +11, self.picImageView.bottom -20-20, 120, 20)];
        _timeLab.font = SKFONT_Semibold(10);
        _timeLab.textAlignment = NSTextAlignmentLeft;
        _timeLab.lineBreakMode = NSLineBreakByTruncatingTail;
        _timeLab.textColor = SKColorWithHex(0x888888);
        _timeLab.hidden = YES;
        [self.bgView addSubview:_timeLab];
    }
    return _timeLab;
}

-(UILabel*)amountLab
{
    if (!_amountLab) {
        _amountLab = [[UILabel alloc] initWithFrame:CGRectMake(self.picImageView.right +11, self.picImageView.bottom -20, 120, 20)];
        _amountLab.font = SKFONT_Semibold(10);
        _amountLab.textAlignment = NSTextAlignmentLeft;
        _amountLab.lineBreakMode = NSLineBreakByTruncatingTail;
        _amountLab.textColor = SKColorWithHex(0x888888);
        [self.bgView addSubview:_amountLab];
    }
    return _amountLab;

}
-(UILabel*)stateLab
{
    if (!_stateLab) {
        _stateLab = [[UILabel alloc] initWithFrame:CGRectMake(self.amountLab.right +15, self.picImageView.bottom -20, self.bgView.width - self.amountLab.right - 5 -25, 20)];
        _stateLab.font = SKFONT_Semibold(10);
        _stateLab.textAlignment = NSTextAlignmentRight;
        _stateLab.lineBreakMode = NSLineBreakByTruncatingTail;
        _stateLab.text = @"正在火速发货中";
        _stateLab.textColor = SKColorWithHex(0xFF2424);
        _stateLab.hidden =YES;
        [self.bgView addSubview:_stateLab];
    }
    return _stateLab;
}

-(UIButton*)complexBtn
{
    if (!_complexBtn) {
        NSString * totalAmtStr = @"补交尾款";
        UIFont * font = SKFONT_Semibold(10);
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(self.bgView.width - 60 - 10, self.picImageView.bottom - 20, 60 , 20);
        [button setTitle:totalAmtStr forState:UIControlStateNormal];
        button.titleLabel.font = font;
        [button setTitleColor:SKRGBColor(255, 255, 255) forState:UIControlStateNormal];
        button.layer.cornerRadius = button.height/2.0;
        button.layer.masksToBounds = YES;
        button.backgroundColor = SKColorWithHex(0xE33030);
        [button addTarget:self action:@selector(complexDeal:) forControlEvents:UIControlEventTouchUpInside];
        button.hidden = YES;
        _complexBtn = button;
        [self.bgView addSubview:button];
    }
    return _complexBtn;
}
- (UIButton *)repayBtn {
    if (!_repayBtn) {
        _repayBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _repayBtn.bounds = self.complexBtn.bounds;
        [_repayBtn setTitle:@"申请退款" forState:UIControlStateNormal];
        _repayBtn.titleLabel.font = SKFONT_Semibold(10);
        _repayBtn.layer.cornerRadius = _repayBtn.height * 0.5;
        _repayBtn.layer.masksToBounds = true;
        _repayBtn.backgroundColor = SKColorWithHex(0xE33030);
        _repayBtn.hidden = true;
        [_repayBtn addTarget:self action:@selector(applyForRepayButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.bgView addSubview:_repayBtn];
    }
    return _repayBtn;
}
-(void)complexDeal:(UIButton*)sender
{
    //    TODO: 1 尾款补交  2 复制单号  3 联系客服
    if (self.myBlock) {
        self.myBlock(_cellModel, sender.tag);
    }
}

/// 申请退款
/// @param sender sender
- (void)applyForRepayButtonClick:(UIButton *)sender {
    // 申请退款
    NSInteger type = [self.cellModel.type integerValue];
    if (type == 1) {
        // 拍卖
        NSString *msg = @"此商品为拍卖商品\n保证金将不支持货款退回\n是否仍要继续？";
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:msg preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        SKWeakSelf;
        UIAlertAction *sure = [UIAlertAction actionWithTitle:@"继续" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
            // 商城购买
            if (weakSelf.applyRejectPayBlock) {
                weakSelf.applyRejectPayBlock();
            }
        }];
        [alert addAction:cancel];
        [alert addAction:sure];
        [[UIApplication sharedApplication].keyWindow.rootViewController presentViewController:alert animated:true completion:nil];
    }else if (type == 2) {
        // 商城购买
        if (self.applyRejectPayBlock) {
            self.applyRejectPayBlock();
        }
    }else if (type == 3) {
        // 积分
        if (self.applyRejectPayBlock) {
            self.applyRejectPayBlock();
        }
    }else {}
}
-(void)setCellModel:(OrderData *)cellModel
{
    _cellModel = cellModel;
    if ([cellModel.type isEqualToString:@"1"]) {
        self.amountLab.text = [NSString stringWithFormat:@"成交金额:%d",[cellModel.money intValue]];
    }
    else if([cellModel.type isEqualToString:@"2"])
    {
        self.amountLab.text = [NSString stringWithFormat:@"成交金额:%@",cellModel.money];
    }else if ([cellModel.type isEqualToString:@"3"]) {
        self.amountLab.text = [NSString stringWithFormat:@"兑换积分:%@",cellModel.jifen];
    }else {}
    //    status: 1待付款2待发货3已发货4已签收5已失效
    self.stateLab.hidden = true;
    self.complexBtn.hidden = true;
    self.timeLab.hidden = true;
    self.repayBtn.hidden = true;
    switch ([cellModel.status intValue]) {
        case 1:
        {
            self.timeLab.hidden = false;
            self.amountLab.textColor = [UIColor redColor];
            self.complexBtn.hidden = false;
            [self.complexBtn setTitle:@"补交尾款" forState:UIControlStateNormal];
            self.complexBtn.tag = 1;
        }break;
        case 2:
        {
            self.repayBtn.hidden = false;
            self.repayBtn.frame = self.complexBtn.frame;
        }break;
        case 3:
        {
            self.complexBtn.hidden = false;
            [self.complexBtn setTitle:@"复制单号" forState:UIControlStateNormal];
            self.complexBtn.tag = 2;
            self.repayBtn.hidden = false;
            self.repayBtn.frame = CGRectMake(self.complexBtn.x - self.complexBtn.width - 10, self.complexBtn.y, self.complexBtn.width, self.complexBtn.height);
        }break;
        case 4:
        {
            self.complexBtn.hidden = false;
            [self.complexBtn setTitle:@"联系客服" forState:UIControlStateNormal];
            self.complexBtn.tag = 3;
            self.repayBtn.hidden = false;
            self.repayBtn.frame = CGRectMake(self.complexBtn.x - self.complexBtn.width - 10, self.complexBtn.y, self.complexBtn.width, self.complexBtn.height);
        }break;
        case 5:
        {
            self.stateLab.hidden = false;
            self.stateLab.text = @"已过期";
            self.stateLab.textColor = SKColorWithHex(0x999999);
        }break;
        case 6: {
            // 申请退款 已申请
            self.stateLab.hidden = false;
            self.stateLab.text = @"退款申请已申请";
        }break;
        case 7: {
            // 退款申请 通过
            self.stateLab.hidden = false;
            self.stateLab.text = @"退款申请已通过";
        }break;
        case 8: {
            // 退款申请 驳回
            self.repayBtn.hidden = true;
//            self.repayBtn.frame = self.complexBtn.frame;
        }break;
        default:
            break;
    }
    NSInteger type = [self.cellModel.type integerValue];
    if (type == 3) {
        self.repayBtn.hidden = true;
        if ([self.cellModel.status integerValue] == 2) {
            self.stateLab.hidden = false;
        }
    }
    [self.picImageView sd_setImageWithURL:[NSURL URLWithString:SafeString(cellModel.goodsInfo.img)] placeholderImage:PlaceholderImage completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        
    }];
    self.titleLab.text = SafeString(cellModel.goodsInfo.name);
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
